package com.example.websocket.threadTest;

public class EnergyTransferTask implements Runnable {

    private EnergySystem energySystem;

    private int fromBoxIndex;

    private double maxAmount;

    private int DELAY = 10;

    public EnergyTransferTask(EnergySystem energySystem, int fromBoxIndex, double maxAmount) {
        this.energySystem = energySystem;
        this.fromBoxIndex = fromBoxIndex;
        this.maxAmount = maxAmount;
    }

    @Override
    public void run() {
        try {
            while (true) {
                int toBox = (int) (energySystem.getBoxAmount() * Math.random());
                double amount = maxAmount * Math.random();
                energySystem.transfer(fromBoxIndex, toBox, amount);
                Thread.sleep((long) (DELAY * Math.random()));
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
